﻿using HarmonyLib;
using RimWorld;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UnityEngine;
using Verse;

namespace Meow
{
    public class CustomThingDef : ThingDef
    {
        public bool followStuffColor = true;
    }

    [HarmonyPatch(typeof(ThingMaker))]
    [HarmonyPatch("MakeThing")]
    public static class ThingMakeColorHook
    {
        [HarmonyPostfix]
        private static void Postfix(ref Thing __result)
        {
            if (__result is ThingWithComps thingWithComps && thingWithComps.def is CustomThingDef customThingDef && !customThingDef.followStuffColor)
            {
                thingWithComps.SetColor(Color.white);
                thingWithComps.SetColor(Color.black);
                thingWithComps.SetColor(Color.white);
            }
        }
    }


    [HarmonyPatch(typeof(PawnApparelGenerator))]
    [HarmonyPatch("GenerateStartingApparelFor")]
    public static class PawnGenColorHook
    {
        [HarmonyPostfix]
        private static void Postfix(ref Pawn pawn)
        {
            if (pawn.apparel == null)
            {
                return;
            }

            List<Apparel> wornApparel = pawn.apparel.WornApparel;
            for (int i = 0; i < wornApparel.Count; i++)
            {
                if (wornApparel[i].def is CustomThingDef customThingDef && !customThingDef.followStuffColor)
                {
                    wornApparel[i].SetColor(Color.white);
                    wornApparel[i].SetColor(Color.black);
                    wornApparel[i].SetColor(Color.white);
                }
            }
        }
    }


    [HarmonyPatch(typeof(GenRecipe))]
    [HarmonyPatch("PostProcessProduct")]
    public static class ProductFinishGenColorHook
    {
        [HarmonyPrefix]
        private static void Prefix(ref Thing product)
        {
            if (product is ThingWithComps thingWithComps && thingWithComps.def is CustomThingDef customThingDef && !customThingDef.followStuffColor)
            {
                thingWithComps.SetColor(Color.white);
            }
        }
    }
}
